# this class takes a stack of and array of html element names and it's output....
#
#
#
# When an HTML element is opened it is added to the stack
# say we have the stack (Think vertically):
#
# we read in previously:
#
#  <div>
#     <span>
#
# Then we add the element to the stack with it's closing element
#  Stack:
#     ["<div>", "</div>"]    --array position 1
#     ["<span>","</span>"]   --array position 0
#
# stackobject.closing_contents() would return the string:  "</div></span>" 
# while keeping the data still in the stack.
# 
# stackobject.beginning_contents() would return the string: "<div><span>"
# while keeping the data stll in the stack
#



class html_element_stack:
  def __init__(self):
    self.inner_contents = []

  def push(self, html_tag_list):
    if len(html_tag_list) > 2:
      print str(list)+" not added.  The list to put on a stack can only have 2 elements e.q. ["+'"div":"</div>"]'
      return false
    else:
      self.inner_contents.append( html_tag_list )
      return True;

  def pop(self): #returns and removes info from stack
    #print str( len( self.inner_contents ) )
    item = self.inner_contents[ len( self.inner_contents ) -1 ]
    new_contents = []
    for x in self.inner_contents:
      if x != item:
        new_contents.append(x)
    self.inner_contents = new_contents
    return item

  def closing_contents(self):
    pos = len( self.inner_contents ) -1
    return_string = ""
    while pos >= 0:
      return_string += self.inner_contents[pos][1]
      pos -= 1
    return return_string
   
  def opening_contents(self):
    return_string = ""
    for t in self.inner_contents:
      return_string += t[0]
    return return_string
    
if __name__=="__main__":
  print "Filling stack...\n"
  x = html_element_stack()
  x.push( ["<div>","</div>"] )
  x.push( ["<span>","</span>"] )
  print x.beginning_contents()
  print x.closing_contents()
  print x.pop()
  print "AFTER POP: "+str( x.inner_contents )
  print x.pop()
  print "AFTER POP: "+str( x.inner_contents )
  
